<?php
/**
 * 功能：
 * 说明：
 * 创建日期：
 * 更新日期：
 * 作者：Mirze
 * 补充说明： 
 * 
 */
require_once 'db_abstract.php';

class mysql extends db_abstract
{

	/**  **/
	public function __construct($dsn=null, $id=null)
	{
		$dsn["db_host"] = "localhost";
		$dsn["db_port"] = "3306";
		$dsn["db_user"] = "root";
		$dsn["db_pwd"] = "123456";
		$dsn["db_name"] = "test";
		$dsn["db_charset"] = "UTF8";
		$this->_dsn = $dsn;
	}

	function connect($pconnect = false, $force_new = false)
    {
        if (is_resource($this->_conn)) { return; }

        $this->_last_err = null;
        $this->_last_err_code = null;

        if (isset($this->_dsn['db_port']) && $this->_dsn['db_port'] != '')
        {
            $host = $this->_dsn['db_host'] . ':' . $this->_dsn['db_port'];
        } else {
            $host = $this->_dsn['db_host'];
        }

        if (! isset($this->_dsn['db_user']))
        {
            $this->_dsn['db_user'] = '';
        }

        if (! isset($this->_dsn['db_pwd']))
        {
            $this->_dsn['db_pwd'] = '';
        }

        if ($pconnect)
        {
            $this->_conn = mysql_pconnect($host, $this->_dsn['db_user'], $this->_dsn['db_pwd'], $force_new);
        }
        else
        {
            $this->_conn = mysql_connect($host, $this->_dsn['db_user'], $this->_dsn['db_pwd'], $force_new);
        }

        if (! is_resource($this->_conn))
        {
            throw new throw_exception('CONNECT DATABASE', mysql_error(), mysql_errno());
        }

        if (! empty($this->_dsn['db_name']))
        {
            $this->execute('USE ' . $this->qid($this->_dsn['db_name']));
        }

        if (isset($this->_dsn['db_charset']) && $this->_dsn['db_charset'] != '')
        {
            $charset = $this->_dsn['db_charset'];
            $this->execute("SET NAMES '" . $charset . "'");
        }
    }

    function pconnect()
    {
        $this->connect(true);
    }

    function nconnect()
    {
        $this->connect(false, true);
    }

    function close()
    {
        if (is_resource($this->_conn))
        {
            mysql_close($this->_conn);
        }
        parent::_clear();
    }

	function identifier($name)
    {
        return ($name != '*') ? "`{$name}`" : '*';
    }

	function execute($sql, $inputarr = null)
    {
        if (! $this->_conn)
        {
            $this->connect();
        }

        $result = mysql_query($sql, $this->_conn);
        if (is_resource($result))
        {
            //return new QDB_Result_Mysql($result, $this->_fetch_mode);
			$this->_handle = $result;
			$this->fetch_mode = 2;
			return $this;
        } elseif ($result) {
            $this->_last_err = null;
            $this->_last_err_code = null;
            return $result;
        } else {
            $this->_last_err = mysql_error($this->_conn);
            $this->_last_err_code = mysql_errno($this->_conn);
            $this->_has_failed_query = true;

            if ($this->_last_err_code == 1062)
            {
                throw new throw_exception($sql, $this->_last_err, $this->_last_err_code);
            } else {
                throw new throw_exception($sql, $this->_last_err, $this->_last_err_code);
            }
        }
    }

	function fetchRow()
	{
		if ($this->fetch_mode == parent::FETCH_MODE_ASSOC) {
			$row = mysql_fetch_assoc($this->_handle);
			if ($this->result_field_name_lower && $row)
			{
				return array_change_key_case($row, CASE_LOWER);
			} else {
				return $row;
			}
		} else {
			return mysql_fetch_array($this->_handle);
		}
	}


	/**  **/
	public function __destruct()
	{		
	}

}
?>